/*
 * Copyright (C) 2012 monfeliscatus@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package felicatus.widgets;

import java.util.Date;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.util.Log;
import android.widget.RemoteViews;

public class TodayDateWidgetWide extends AppWidgetProvider 
{
	private static final String TAG = "AppWidgetProviderWide";
	
	private boolean	AlarmIsOn = false;
	private boolean	AlarmTurnOn = false;
	private Intent	serviceIntent = null;

	protected RemoteViews updateDateTimeText ( Context context )
	{
    	Date date = new Date();
    	Time now = new Time(Time.getCurrentTimezone());
    	now.setToNow();
    	CharSequence csDateTextY = DateFormat.format( "yyyy", date );
    	CharSequence csDateTextM = DateFormat.format( "MM/dd", date );
    	CharSequence csDateTextT = DateFormat.format( "kk:mm", date );	// "kk:mm:ss", date );
		
		//Log.i( TAG, "updateDateTimeText [" + csDateTextY + "-" + csDateTextM + " / " + csDateTextT + "]");
		
		RemoteViews updateViewDate = new RemoteViews(context.getPackageName (), R.layout.widgetwide );
		updateViewDate.setTextViewText(R.id.WidgetTextviewY, csDateTextY );
		updateViewDate.setTextViewText(R.id.WidgetTextviewM, csDateTextM );
		updateViewDate.setTextViewText(R.id.WidgetTextviewT, csDateTextT );
		
		return updateViewDate;
	}
	
	public void doUpdate ( Context context )
	{
		RemoteViews	updateViewDate;
		updateViewDate = updateDateTimeText( context );
		
		ComponentName thisWidget = new ComponentName( context, TodayDateWidgetWide.class.getName() );
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		int [] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		appWidgetManager.updateAppWidget(appWidgetIds, updateViewDate);		
	}
	
	@Override
    public void onReceive(Context context, Intent intent)
    {
		super.onReceive(context, intent);
    }	
	
	@Override
	public void onEnabled(Context context)
	{
		super.onEnabled(context);
		
		RemoteViews	updateViewDate;
		updateViewDate = updateDateTimeText( context );
		
		ComponentName thisWidget = new ComponentName( context, TodayDateWidgetWide.class.getName() );
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		int [] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		appWidgetManager.updateAppWidget(appWidgetIds, updateViewDate);
		
		AlarmBR.widgetWide = this;
		ScreenOnOffReceiver.widgetWide = this;
		
		AlarmIsOn = true;
		AlarmBR.SetAlarm( context );

		if ( serviceIntent == null )
		{
			serviceIntent = new Intent(context, ScreenOnOffReceiver.class);
			context.startService( serviceIntent );
		}
	}
	
	@Override
	public void onDisabled(Context context) 
	{
		super.onDisabled(context);
		
		if ( serviceIntent != null )
		{
			context.stopService( serviceIntent );
			serviceIntent = null;
		}
		AlarmIsOn = false;
		AlarmBR.ClearAlram(context);
	}
   
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    {
    	super.onUpdate(context, appWidgetManager, appWidgetIds);
    	
    	RemoteViews	updateViewDate;
		updateViewDate = updateDateTimeText( context );
		appWidgetManager.updateAppWidget(appWidgetIds, updateViewDate);
    }
    
    public void pauseAlarm ( Context context )
    {
    	if ( AlarmIsOn && !AlarmTurnOn )
    	{
    		AlarmBR.ClearAlram(context);
    		AlarmTurnOn = true;
    	}
    }
    public void resumeAlarm ( Context context )
    {
    	if ( AlarmIsOn && AlarmTurnOn )
    	{
    		AlarmBR.SetAlarm( context );
    		AlarmTurnOn = false;
    	}
    }
}


